<?php

/**
 * 通知
 * File Name：MessageController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-19 16:10:12
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MessageController extends UserLoginBaseC {

    //通知列表
    public function actionList() {
        $model = MessageModel::model();
        $where = array(
            'order' => 'datetime desc,nid desc',
            'userid' => Yii::app()->user->id,
        );
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 10;
        $criteria = $model->where($where);
        $count = $model->count($criteria);
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);
        //打开次页面表示已经阅读
        $nids = array();
        foreach ($data as $k => $r) {
            $data[$k]['extend_params'] = unserialize($r['extend_params']);
            if ($r['is_read']) {
                continue;
            }
            $nids[$r['nid']] = $r['nid'];
        }
        if (!empty($nids)) {
            $model->noticeRead($nids, Yii::app()->user->uid);
        }
        $this->assign('Page', $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

    //删除单条通知
    public function actionDelete() {
        $model = MessageModel::model();
        $nid = (int) Yii::app()->request->getParam('nid');
        if (empty($nid)) {
            $this->error('通知不存在！');
        }
        $noticeInfo = $model->find($model->where(array('userid' => Yii::app()->user->id, 'nid' => $nid)));
        if (empty($noticeInfo)) {
            $this->error('通知不存在！');
        }
        if ($model->noticeDel(Yii::app()->user->id, $nid)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //用户头部信息通知
    public function actionNotice() {
        $this->layout = false;
        $model = MessageModel::model();
        $where = array(
            'order' => 'datetime desc,nid desc',
            'userid' => Yii::app()->user->id,
            'is_read' => 0,
            'limit' => 5,
        );
        $criteria = $model->where($where);
        $data = $model->findAll($criteria);
        $nids = array();
        foreach ($data as $k => $r) {
            $data[$k] = $r->attributes;
            $data[$k]['extend_params'] = unserialize($r['extend_params']);
            if ($r['is_read']) {
                continue;
            }
            $nids[$r['nid']] = $r['nid'];
        }
        if (!empty($nids)) {
            //$model->noticeRead($nids, Yii::app()->user->uid);
        }
        $this->assign('data', $data);
        $this->render();
    }

    //删除全部已读消息
    public function actionDeleteall() {
        if (MessageModel::model()->delReadAll(Yii::app()->user->id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 模板处理
     * @param type $data
     * @return type
     */
    protected function templates($data, $directory = 'lib') {
        $layout = $this->layout;
        $this->layout = false;
        $view = "{$directory}/{$data['typeid']}";
        $this->assign($data);
        $templates = $this->render($view, null, false);
        $this->layout = $layout;
        return $templates;
    }

}
